package xyz.jason.mybatisgenerator.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xyz.jason.mybatisgenerator.config.PathConfig;
import xyz.jason.mybatisgenerator.util.FileUtils;
import xyz.jason.mybatisgenerator.util.GeneratorUtil;
import xyz.jason.mybatisgenerator.util.SchedulingUtil;
import xyz.jason.mybatisgenerator.vo.DataEntity;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

/**
 * @ProjectName: mybatis-generator
 * @Package: xyz.jason.mybatisgenerator.controller
 * @ClassName: ApiController
 * @Author: Jason · L
 * @Description: api
 * @Date: 2021/4/6 15:06
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/api")
public class ApiController {

    @Autowired
    PathConfig pathConfig;


    @PostMapping("/index")
    public ModelAndView index(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("login");
        DataEntity dataEntity = new DataEntity();
        dataEntity.defaultEntity();
        mav.addObject("dataEntity",dataEntity);
        return mav;
    }

    @PostMapping("/clear")
    public String clear()  {
        Stream.of(pathConfig.getPath(),pathConfig.getZipPath())
                .forEach(FileUtils::deleteMkDirs);
        return "forward:/api/index";
    }




    @GetMapping("/downLoad")
    public ResponseEntity<byte[]> downLoad(@RequestParam("fileName") String fileName) throws IOException {
        byte[] bytes = Files.readAllBytes(Paths.get( pathConfig.getZipPath() + fileName));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentLength(bytes.length);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        SchedulingUtil.remove(()->Stream.of(pathConfig.getZipPath(),pathConfig.getPath()+pathConfig.getResources(),pathConfig.getPath()+pathConfig.getJava()).forEach( FileUtils::deleteMkDirs));
        return new ResponseEntity<byte[]>(bytes,headers,HttpStatus.OK);
    }


    @PostMapping("/generate")
    public ModelAndView generate(@ModelAttribute @Validated DataEntity dataEntity){
        ModelAndView mav = new ModelAndView("login.html");
        try{
            dataEntity.check();
            GeneratorUtil.generator(pathConfig,dataEntity);
            String fileName = FileUtils.doZip(pathConfig.getZipPath(),pathConfig.getPath());
            mav.addObject("download", pathConfig.getDownLoadUrl()+fileName);
            mav.addObject("dataEntity",dataEntity);
        }catch (NullPointerException e){
            log.error("生成代码错误",e);
            mav.addObject("error",e.getMessage());
        }catch (Exception e){
            log.error("生成代码错误",e);
            mav.addObject("error","生成代码错误");
        }
        return mav;
    }









}
